package com.eascs.banknet.boc.service.ftp;

import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.banknet.boc.dao.LoanapplyDao;
import com.eascs.banknet.boc.service.BOCAbstractService;
import com.eascs.banknet.boc.vo.FtpFileTemplage;
import com.eascs.banknet.common.entity.LoanUserEntity;
import com.eascs.banknet.common.entity.LoanapplyEntity;
import com.eascs.banknet.constant.FileTypeConstants;
import com.eascs.banknet.log.entity.LogEntity;

@Service("bOCDKHTDownloadService")
public class BOCDKHTDownloadService extends BOCAbstractService {
	private static final Logger logger = LoggerFactory.getLogger(BOCDKHTDownloadService.class);
	@Autowired
	private LoanapplyDao loanapplyDao;

	@Override
	public Map<String, String> dowork() {
		// 从中行FTP拉取借款合同与借据等类型的附件
		logger.info("从中行FTP拉取借款合同与借据,跑批开始...");
		List<LoanUserEntity> dataList = loanapplyDao.getPullDKHTDataList();
		if (dataList.isEmpty()) {
			logger.info("中行FTP拉取借款合同与借据， 没有符合条件的数据");
			return null;
		}
		LogEntity logEntity = initLogData();
		for (LoanUserEntity loanUserEntity : dataList) {
			try {
				String loanapplyNo = loanUserEntity.getLoanapplyNo();
				String appno = loanUserEntity.getBankApplyno();
				LoanapplyEntity loanapplyEntity = loanapplyDao.getById(LoanapplyEntity.class, loanapplyNo);
				FtpFileTemplage ftpFile = bocUtil.getBocFtpFileTemplate(FileTypeConstants.DALIST_TYPE_REFUNDCONTRACTBAK);
				boolean saveResult = bocNetService.getFtpAttachment(ftpFile, appno, loanapplyNo, loanapplyEntity.getBankOsn());
				if (!saveResult) {
					logger.info("中行FTP拉取借款合同与借据失败， appno:[{}], loanapplyNo:[{}], fileType:[{}]", appno, loanapplyNo, FileTypeConstants.DALIST_TYPE_REFUNDCONTRACTBAK);
					logService.writeLog(logEntity, appno, LogEntity.RESULT_TYPE_ERROR, null);
				} else {
					logger.info("中行FTP拉取借款合同与借据成功， appno:[{}], loanapplyNo:[{}], fileType:[{}]", appno, loanapplyNo, FileTypeConstants.DALIST_TYPE_REFUNDCONTRACTBAK);
					logService.writeLog(logEntity, appno, LogEntity.RESULT_TYPE_SUCCESS, null);
				}
			} catch (Exception e) {
				logger.error("获取中行FTP拉取借款合同与借据出现异常", e);
			}
		}
		logger.info("从中行FTP拉取借款合同与借据,跑批结束...");
		return null;
	}

	@Override
	public LogEntity initLogData() {
		LogEntity logEntity = new LogEntity();
		logEntity.setBankType(LogEntity.BANK_TYPE_BOC);
		logEntity.setTransType("DKHT");
		logEntity.setTypeName("电子合同");
		logEntity.setStartTime(new Date());
		return logEntity;
	}
}
